<?php
namespace Admin\Controller;
use Think\Controller;
class CheckController extends CommonController {
	//拆分页面
	public function chaifen(){
		$this -> display();
	}

	//拆分操作
	public function actcf(){
		$k = true;
		$res = M('param') -> field('param1,param2,param12') -> find(1);
		if($res['param1'] < $res['param12']){
			$this -> error('市价未达到拆分等级');
		}else{
			M() -> startTrans();			
			$res2 = M('users') -> field('option_num,max_amount,user_id') -> join('zx_user_type on zx_users.reg_type=zx_user_type.id') -> select();
			foreach($res2 as $k=>$v){
				if($v['option_num'] == 0 || $v['option_num'] == $v['max_amount']){
					$k = true;
				}else{
					$option_num = $v['option_num'] * I('post.beishu');

					if($option_num > $v['max_amount']){
						$option_num = $v['max_amount'];
					}
					$data = ['option_num'=>$option_num];
					$res3 = M('users') -> data($data) -> where('user_id=%d',[$v['user_id']]) -> save();
					// //封装前台日志
					$m6 = M('account_log');
					 $num = M('k_log') -> where('k_money = %s','0.2') -> count()-1;
					 	$ac_log['user_id'] = $v['user_id'];
					 	$ac_log['change_time']	 = time();
					 	$ac_log['change_type']	= 4;
					 	$ac_log['change_desc'] = '第'.$num.'拆分';			
						$res6 = $m6 -> add($ac_log);
						
					if($res3>0 && $res6>0){
						$k = true;
					}else{
						$k = false;
						$this -> error('拆分在'.$v['user_id'].'失败');
					}
				}
			}

			if($k){
				$res4 = M('param') -> data(['param1'=>$res['param2']]) -> where('id=%d',[1]) -> save();
				if($res4>0){
					$k = true;
				}else{
					M() -> rollback();
					$this -> error('返回原始股价失败，操作未成功');
				}
				//封装日志
				//封装后台日志
				$admin_log = M('admin_log');
				$ad_log['user_name'] = $_SESSION['admin']['user_name'];
				$ad_log['log_time'] = time();
				$ad_log['user_id'] = $_SESSION['admin']['user_id'];
				$ad_log['log_info'] = '拆分操作';
				$ad_log['ip_address']= get_client_ip();
				 $admin_log -> add($ad_log);
				M() -> commit();
				$this -> success('拆分成功');
			}else{
				M() -> rollback();
				$this -> error('拆分失败');
			}
		}
	}

	//审核业绩
	public function shenhe(){
		$res = M('user_type') -> field('id,name') -> select();
		$this -> assign('res',$res);
		$this -> display();
	}

	//审核操作
	public function actsh(){
		$res = M('users') -> field('user_id,user_type,reg_type,left_num,right_num') -> where('user_type=%d',[I('post.level')]) -> select();
		// dump($res);die;
		M() -> startTrans();
		foreach($res as $k => $v){
			if($v['user_type'] == $v['reg_type']){
				$k = true;
				// dump($v);die;
			}else{
				$arr = [$v['left_num'],$v['right_num']];
				sort($arr);
				$min = $arr[0];
				// dump($min);
				// dump(I('post.'));
				if($min<I('post.zhibiao')){
					$res2 = M('users') -> data(['user_type' => $v['reg_type']]) -> where('user_id=%d',[$v['user_id']]) -> save();
					echo M() -> _SQL();
					if(!$res2){
						M() -> rollback();
						$this -> error('降级出错，请重新设置指标');
					}
				}
			}
		}
		// echo 'jiesu';die;
		if($k){
			M() -> commit();
			$this -> success('审核成功');
		}else{
			M() -> rollback();
			$this -> error('审核失败');
		}
	}
}